package com.dk.controller;

import com.dk.Vo.UUserVo;
import com.dk.pojo.RRole;
import com.dk.pojo.TDep;
import com.dk.pojo.UUser;
import com.dk.service.RRoleServiceI;
import com.dk.service.TDepServiceI;
import com.dk.service.UUserServiceI;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("uUserController")
public class UUserController {

    //用户表
    @Autowired
    private UUserServiceI uUserServiceI;

    //角色表
    @Autowired
    private RRoleServiceI rRoleServiceI;

    //部门
    @Autowired
    private TDepServiceI tDepServiceI;

    //跳转用户页面
    @RequestMapping("list")
    public String listGet(Model model){
        //部门查询
        List<TDep> tDeps = tDepServiceI.getInfoServer();
        model.addAttribute("tDeps",tDeps);
        return "user/list";
    }

    //查询用户
    @RequestMapping("getInfoServer")
    @ResponseBody
    public Map<String,Object> getInfoServer(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize,String uname,Integer depId){
        PageInfo<UUserVo> page = uUserServiceI.getInfoServer(pageNum,pageSize,uname,depId);
        Map<String,Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getList());
        return map;
    }

    //跳转用户角色增加页面
    @RequestMapping("insertInfo")
    public String insertInfo(Model model){
        //角色查询
        List<RRole> role = rRoleServiceI.getInfoServer();
        model.addAttribute("roles",role);
        //部门查询
        List<TDep> tDeps = tDepServiceI.getInfoServer();
        model.addAttribute("tDeps",tDeps);
        return "user/add";
    }
    //确认增加用户角色
    @RequestMapping("addById")
    @ResponseBody
    public int addById(UUser uUser,@RequestParam("rid[]")Integer[] rid){
        uUserServiceI.addById(uUser,rid);
        return 1;
    }

    @RequestMapping("updateInfo/{uid}")
    public String updateInfo(@PathVariable("uid")Integer uid,Model model){

        //用户表查询
       UUser uUser =  uUserServiceI.getById(uid);
       model.addAttribute("user",uUser);

       //查询所有的角色
        List<RRole> roles = rRoleServiceI.getInfoServer();
        model.addAttribute("roles",roles);

        //根据用户id 查询对应的角色id
        List<RRole> role =rRoleServiceI.getUserById(uid);
        model.addAttribute("role",role);

        //部门查询
        List<TDep> tDeps = tDepServiceI.getInfoServer();
        model.addAttribute("tDeps",tDeps);

        return "user/update";
    }

    //确认修改
    @RequestMapping("updateById")
    @ResponseBody
    public int updateById(UUser uUser, @RequestParam("rid[]")Integer[] rid){
        uUserServiceI.updateById(uUser,rid);
        return 1;
    }

    //删除用户
    @RequestMapping("deleteById/{uid}")
    @ResponseBody
    public int deleteById(@PathVariable("uid")Integer uid){
        uUserServiceI.deleteById(uid);
        return 1;
    }
}
